<template>
	<view :tempFilePath="tempFilePath" :change:tempFilePath="canvas.getVideoCanvas" :s="s" :change:s="canvas.getS">
	</view>
</template>
 <script>
		export default {
			mounted() {

			},
			props:{
				tempFilePath:'',
				s:{
					default:1
				}
			},
			methods:{
				getVideoPoster(path){
					uni.$emit('res',path)
				}
			}
		}




</script>
<script  module="canvas" lang="renderjs">
	var s = 1
	export default {
		methods: {
			getS(nv) {
				s = nv || 1
			},
			async getVideoCanvas(nV, oV, ownerInstance) {
				if (nV) {
					const frame = await this.captureFrame(nV, s)
					this.$ownerInstance.callMethod('getVideoPoster',frame )

				}
			},
			captureFrame(vdoSrc, time = 0) {
				return new Promise((resolve) => {
					const vdo = document.createElement('video')
					vdo.currentTime = time
					vdo.crossOrigin = 'anonymous'
					vdo.muted = true
					vdo.autoplay = true
					vdo.src = vdoSrc
					vdo.oncanplay = async () => {
						const frame = await this.drawVideo(vdo)
						resolve(frame)
					}
				})
			},

			drawVideo(vdo) {
				return new Promise((resolve) => {
					const cvs = document.createElement('canvas')
					const ctx = cvs.getContext('2d')
					cvs.width = vdo.videoWidth
					cvs.height = vdo.videoHeight
					ctx.drawImage(vdo, 0, 0, cvs.width, cvs.height)
					const base64 = cvs.toDataURL()
					resolve(base64)
					return
					// 创建blob对象
					cvs.toBlob((blob) => {
						resolve(URL.createObjectURL(blob))
					})
				})
			}

		}
	}
</script>
<style>
</style>
